package com.woniuxy.broker.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.woniuxy.entity.broker.Broker;
import com.woniuxy.entity.broker.Question;
import com.woniuxy.entity.broker.Recommend;
import com.woniuxy.entity.broker.Server;
import com.woniuxy.broker.service.BrokerService;
import com.woniuxy.broker.service.QuestionService;
import com.woniuxy.broker.service.RecommendService;
import com.woniuxy.broker.service.ServerService;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.enums.Exchange;
import com.woniuxy.group.DeleteGroup;
import com.woniuxy.group.InsertGroup;
import com.woniuxy.group.SelectGroup;
import com.woniuxy.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Positive;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author booty
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/broker")
@DefaultProperties(defaultFallback = "defaultGlobalFallBack", commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
})
@Api(tags = "经纪人接口")
public class BrokerController {

    @Resource
    private BrokerService brokerService;
    @Resource
    private QuestionService questionService;
    @Resource
    private RecommendService recommendService;
    @Resource
    private ServerService serverService;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    RabbitTemplate.ConfirmCallback confirmCallback;
    @Resource
    RabbitTemplate.ReturnCallback returnCallback;


    public ResponseResult<?> defaultGlobalFallBack() {
        return new ResponseResult<>(555, "服务调用响应慢或超时，请稍后再试");
    }


//--------------------------Broker------------------------

    @ApiOperation(value = "查询单个经纪人的接口", notes = "动态查询，根据条件传入参数，确保条件下只有一条记录，建议传入每次传入status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "avatar", value = "头像"),
            @ApiImplicitParam(name = "regtime", value = "注册时间，格式yyyy-MM-dd"),
            @ApiImplicitParam(name = "level", value = "星级"),
            @ApiImplicitParam(name = "shopid", value = "门店id"),
            @ApiImplicitParam(name = "status", value = "是否正常工作，0正常，1离职", required = true)
    })
    @HystrixCommand
    @GetMapping("getSingleBroker")
    public ResponseResult<?> getSingleBroker(@Validated(SelectGroup.class) Broker broker) {
        return new ResponseResult<>(brokerService.getOne(new QueryWrapper<>(broker)));
    }

    @ApiOperation(value = "查询多个经纪人的接口", notes = "动态查询，根据条件传入参数，建议传入每次传入status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, paramType = "int"),
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "avatar", value = "头像"),
            @ApiImplicitParam(name = "regtime", value = "注册时间，格式yyyy-MM-dd"),
            @ApiImplicitParam(name = "level", value = "星级"),
            @ApiImplicitParam(name = "shopid", value = "门店id"),
            @ApiImplicitParam(name = "status", value = "是否正常工作，0正常，1离职", required = true)
    })
    @HystrixCommand
    @GetMapping("getMultipleBroker")
    public ResponseResult<?> getMultipleBroker(@Positive int page, @Positive int size, @Validated(SelectGroup.class) Broker broker) {
        return new ResponseResult<>(brokerService.page(new Page<>(page, size), new QueryWrapper<>(broker)));
    }

    @ApiOperation(value = "添加单个经纪人的接口", notes = "必须传入指定参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "name", value = "姓名", required = true),
            @ApiImplicitParam(name = "phone", value = "电话", required = true),
            @ApiImplicitParam(name = "avatar", value = "头像"),
            @ApiImplicitParam(name = "regtime", value = "注册时间，格式yyyy-MM-dd", required = true),
            @ApiImplicitParam(name = "level", value = "星级", required = true),
            @ApiImplicitParam(name = "shopid", value = "门店id", required = true),
            @ApiImplicitParam(name = "status", value = "是否正常工作，0正常，1离职")
    })
    @HystrixCommand
    @GetMapping("addBroker")
    public ResponseResult<?> addBroker(@Validated(InsertGroup.class) Broker broker) {
        return new ResponseResult<>(brokerService.save(broker));
    }


    @ApiOperation(value = "修改单个经纪人的接口", notes = "动态修改，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "avatar", value = "头像"),
            @ApiImplicitParam(name = "regtime", value = "注册时间，格式yyyy-MM-dd"),
            @ApiImplicitParam(name = "level", value = "星级"),
            @ApiImplicitParam(name = "shopid", value = "门店id"),
            @ApiImplicitParam(name = "status", value = "是否正常工作，0正常，1离职")
    })
    @HystrixCommand
    @GetMapping("updateBroker")
    public ResponseResult<?> updateBroker(@Validated(UpdateGroup.class) Broker broker) {
        return new ResponseResult<>(brokerService.updateById(broker));
    }


    @ApiOperation(value = "删除单个经纪人的接口", notes = "动态软删除，根据条件传入参数，支持根据条件批量修改，若只修改单个确保条件只有一个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "avatar", value = "头像"),
            @ApiImplicitParam(name = "regtime", value = "注册时间，格式yyyy-MM-dd"),
            @ApiImplicitParam(name = "level", value = "星级"),
            @ApiImplicitParam(name = "shopid", value = "门店id"),
            @ApiImplicitParam(name = "status", value = "是否正常工作，0正常，1离职", required = true)
    })
    @HystrixCommand
    @GetMapping("delBroker")
    public ResponseResult<?> delBroker(@Validated(DeleteGroup.class) Broker broker) {
        return new ResponseResult<>(brokerService.updateById(broker));
    }


//--------------------------Question------------------------

    @ApiOperation(value = "查询单个用户咨询的接口", notes = "动态查询，根据条件传入参数，请确保条件只有一个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "userid", value = "用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "reply", value = "经纪人的回答"),
            @ApiImplicitParam(name = "question", value = "用户的提问")
    })
    @HystrixCommand
    @GetMapping("getSingleQuestion")
    public ResponseResult<?> getSingleQuestion(@Validated(SelectGroup.class) Question question) {
        return new ResponseResult<>(questionService.getOne(new QueryWrapper<>(question)));
    }

    @ApiOperation(value = "查询多个用户咨询的接口", notes = "动态查询，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "int"),
            @ApiImplicitParam(name = "size", value = "条数", required = true, paramType = "int"),
            @ApiImplicitParam(name = "userid", value = "用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "reply", value = "经纪人的回答"),
            @ApiImplicitParam(name = "question", value = "用户的提问")
    })
    @HystrixCommand
    @PostMapping("getMultipleQuestion")
    public ResponseResult<?> getMultipleQuestion(@Positive int page, @Positive int size, @Validated(SelectGroup.class) Question question) {
        return new ResponseResult<>(questionService.page(new Page<>(page, size), new QueryWrapper<>(question)));
    }

    @ApiOperation(value = "修改用户咨询的接口", notes = "动态修改，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "userid", value = "用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "reply", value = "经纪人的回答"),
            @ApiImplicitParam(name = "question", value = "用户的提问")
    })
    @HystrixCommand
    @PostMapping("updateQuestion")
    public ResponseResult<?> updateQuestion(@Validated(UpdateGroup.class) Question question) {
        return new ResponseResult<>(questionService.updateById(question));
    }

    @ApiOperation(value = "添加用户咨询的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "reply", value = "经纪人的回答"),
            @ApiImplicitParam(name = "question", value = "用户的提问", required = true)
    })
    @HystrixCommand
    @PostMapping("addQuestion")
    public ResponseResult<?> addQuestion(@Validated(InsertGroup.class) Question question) {

        return new ResponseResult<>(questionService.save(question));
    }

    @ApiOperation(value = "删除用户咨询的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
    })
    @HystrixCommand
    @PostMapping("delQuestion")
    public ResponseResult<?> delQuestion(@Validated(DeleteGroup.class) Question question) {

        return new ResponseResult<>(questionService.removeById(question));
    }

//--------------------------Recommend------------------------


    @ApiOperation(value = "查询单个经纪人推荐楼盘的接口", notes = "动态查询，根据条件传入参数，若只查询单个确保条件只有一个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "buildingid", value = "楼盘id"),
            @ApiImplicitParam(name = "content", value = "推荐内容"),
    })
    @HystrixCommand
    @GetMapping("getSingleRecommend")
    public ResponseResult<?> getSingleRecommend(@Validated(SelectGroup.class) Recommend recommend) {
        return new ResponseResult<>(recommendService.getOne(new QueryWrapper<>(recommend)));
    }

    @ApiOperation(value = "查询多个经纪人推荐楼盘的接口", notes = "动态查询，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "int"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "int"),
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "buildingid", value = "楼盘id"),
            @ApiImplicitParam(name = "content", value = "推荐内容"),
    })
    @HystrixCommand
    @GetMapping("getMultipleRecommend")
    public ResponseResult<?> getMultipleRecommend(@Positive int page, @Positive int size, @Validated(SelectGroup.class) Recommend recommend) {
        return new ResponseResult<>(recommendService.page(new Page<>(page, size), new QueryWrapper<>(recommend)));
    }

    @ApiOperation(value = "添加经纪人推荐楼盘的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brokerid", value = "经纪人id", required = true),
            @ApiImplicitParam(name = "buildingid", value = "楼盘id", required = true),
            @ApiImplicitParam(name = "content", value = "推荐内容", required = true),
    })
    @HystrixCommand
    @PostMapping("addRecommend")
    public ResponseResult<?> addRecommend(@Validated(InsertGroup.class) Recommend recommend) {
        return new ResponseResult<>(recommendService.save(recommend));
    }

    @ApiOperation(value = "修改经纪人推荐楼盘的接口", notes = "动态修改，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "buildingid", value = "楼盘id"),
            @ApiImplicitParam(name = "content", value = "推荐内容"),
    })
    @HystrixCommand
    @PostMapping("updateRecommend")
    public ResponseResult<?> updateRecommend(@Validated(UpdateGroup.class) Recommend recommend) {
        return new ResponseResult<>(recommendService.updateById(recommend));
    }

    @ApiOperation(value = "删除经纪人推荐楼盘的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
    })
    @HystrixCommand
    @PostMapping("delRecommend")
    public ResponseResult<?> delRecommend(@Validated(DeleteGroup.class) Recommend recommend) {
        return new ResponseResult<>(recommendService.removeById(recommend));
    }

//--------------------------Server------------------------

    @ApiOperation(value = "查询单个经纪人服务案例的接口", notes = "动态查询，根据条件传入参数，若只查询单个确保条件只有一个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "userid", value = "被服务的用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "content", value = "服务内容"),
    })
    @HystrixCommand
    @GetMapping("getSingleServer")
    public ResponseResult<?> getSingleServer(@Validated(SelectGroup.class) Server server) {
        return new ResponseResult<>(serverService.getOne(new QueryWrapper<>(server)));
    }

    @ApiOperation(value = "查询多个经纪人服务案例的接口", notes = "动态查询，根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "int"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "int"),
            @ApiImplicitParam(name = "id", value = "主键"),
            @ApiImplicitParam(name = "userid", value = "被服务的用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "content", value = "服务内容"),
    })
    @HystrixCommand
    @GetMapping("getMultipleServer")
    public ResponseResult<?> getMultipleServer(@Positive int page, @Positive int size, @Validated(SelectGroup.class) Server server) {
        return new ResponseResult<>(serverService.page(new Page<>(page, size), new QueryWrapper<>(server)));
    }


    @ApiOperation(value = "添加经纪人服务案例的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "被服务的用户id", required = true),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id", required = true),
            @ApiImplicitParam(name = "content", value = "服务内容", required = true),
    })
    @HystrixCommand
    @PostMapping("addServer")
    public ResponseResult<?> addServer(@Validated(InsertGroup.class) Server server) {
        return new ResponseResult<>(serverService.save(server));
    }


    @ApiOperation(value = "修改经纪人服务案例的接口", notes = "动态修改,根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "userid", value = "被服务的用户id"),
            @ApiImplicitParam(name = "brokerid", value = "经纪人id"),
            @ApiImplicitParam(name = "content", value = "服务内容"),
    })
    @HystrixCommand
    @PostMapping("updateServer")
    public ResponseResult<?> updateBroker(@Validated(UpdateGroup.class) Server server) {
        return new ResponseResult<>(serverService.updateById(server));
    }


    @ApiOperation(value = "删除经纪人服务案例的接口", notes = "根据条件传入参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
    })
    @HystrixCommand
    @PostMapping("delServer")
    public ResponseResult<?> delServer(@Validated(DeleteGroup.class) Server server) {
        return new ResponseResult<>(serverService.removeById(server));
    }

    @ApiOperation(value = "测试向rabbitMQ发送消息", notes = "传入参数消息")
    @ApiImplicitParam(name = "message", value = "消息", required = true)
    @HystrixCommand
    @PostMapping("sendMessage")
    public ResponseResult<?> sendMessage(String message) {
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        message = message + LocalDateTime.now().toString();
        rabbitTemplate.convertAndSend(
                Exchange.BROKER_EXCHANGE_DELAY.getName(),
                Exchange.BROKER_EXCHANGE_DELAY.getRouterKey(),
                message);
        return ResponseResult.SUCCESS;
    }


}

